#include "hplcsparklinkautosamplerparsepacket.h"
#include "hplcsparklinkautosamplerprotocoldata.h"
#include "hplcsparklinkautosamplerpfcid.h"
#include "hplcsparklinkpacketformat.h"
#include "hplcsparklinkcheckpacket.h"
#include "hplcsparklinkmakepacket.h"
#include <QDebug>
#include "misc.h"
#include "convert.h"

#include "modulehplcautosamplercmdid.h"

namespace hplc
{
namespace sparklink
{

HplcSparkLinkAutoSamplerParsePacket::HplcSparkLinkAutoSamplerParsePacket(HplcSparkLinkAutoSamplerProtocolData &data, AutoSamplerParam &param, QObject *parent)
    : BaseParsePacket (data, parent)
    , m_param(&param)
{

}

HplcSparkLinkAutoSamplerParsePacket::~HplcSparkLinkAutoSamplerParsePacket()
{

}

void HplcSparkLinkAutoSamplerParsePacket::parse()
{
    HplcSparkLinkAutoSamplerProtocolData *m_d = static_cast<HplcSparkLinkAutoSamplerProtocolData*>(this->m_d);

    qDebug() << m_d->m_rxPacket.buffer;

    if (m_d->m_rxPacket.buffer.size() == 1) // 回复命令
    {
        m_d->m_checker->replyCheck(m_d->m_rxPacket.buffer.at(0));
        return;
    }

    char *p = m_d->m_rxPacket.buffer.data();
    quint8 id = Convert::BCD2UInt((uint8_t*)(p), 2);
    uint8_t buffer[3];
    Convert::Ascii2Hex(buffer, (uint8_t*)(p + 2), 2);
    quint8 ai = buffer[0];
    quint16 pfc = Convert::BCD2UInt((uint8_t*)(p + 4), 4);

    qDebug() << QString("%1").arg(id, 2, 10, QLatin1Char('0'))
         << QString("%1").arg(ai, 2, 16, QLatin1Char('0'))
         << QString("%1").arg(pfc, 4, 10, QLatin1Char('0'));

    // 是否是自动进样器
    HplcSparkLinkAutoSamplerProtocolData *d = (HplcSparkLinkAutoSamplerProtocolData*)(m_d);
    if (id != d->m_id)
        return;

    m_d->m_checker->replyCheck(pfc);

    p += 8; // 指向数据区域
    switch (pfc)
    {
    case HplcSparkLinkAutoSamplerPfcId_AnalysisTime:
        {
            uint8_t hour = Convert::BCD2UInt((uint8_t*)(p + 1), 1);
            uint8_t minute = Convert::BCD2UInt((uint8_t*)(p + 2), 2);
            uint8_t second = Convert::BCD2UInt((uint8_t*)(p + 4), 2);
            m_param->analysisTime = Misc::toSecond(hour, minute, second);

            qDebug() << hour << minute << second << m_param->analysisTime;

            m_param->_analysisTime.hour = hour;
            m_param->_analysisTime.minute = minute;
            m_param->_analysisTime.second = second;

            QVariant var;
            var.setValue(m_param->_analysisTime);
            emit parseCompleted(hplc::sparklink::CmdID_P_AnalysisTime, var);
        }
        break;
    case HplcSparkLinkAutoSamplerPfcId_LoopVolume:
        {
            m_param->loopVolume = Convert::SparkLinkProtocol_BCD2Int((uint8_t*)(p + 2), 4);

            QVariant var;
            var.setValue(m_param->loopVolume);
            emit parseCompleted(hplc::sparklink::CmdID_P_LoopVolume, var);
        }
        break;
    case HplcSparkLinkAutoSamplerPfcId_FirstSamplePosition:
        {
            m_param->firstSamplePosition.plateNumber
                    = Convert::SparkLinkProtocol_BCD2Int((uint8_t*)(p + 1), 1);
            if (m_param->firstSamplePosition.plateNumber == 3)
                m_param->firstSamplePosition.vialNumber
                        = Convert::SparkLinkProtocol_BCD2Int((uint8_t*)(p + 2), 4);
            else
            {
                m_param->firstSamplePosition.columnRowNumber.columnNumber
                        = Convert::SparkLinkProtocol_BCD2Int((uint8_t*)(p + 2), 2);
                m_param->firstSamplePosition.columnRowNumber.rowNumber
                        = Convert::SparkLinkProtocol_BCD2Int((uint8_t*)(p + 4), 2);
            }

            QVariant var;
            var.setValue(m_param->firstSamplePosition);
            emit parseCompleted(hplc::sparklink::CmdID_P_FirstSamplePosition, var);
        }
        break;
    case HplcSparkLinkAutoSamplerPfcId_LastSamplePosition:
        {
            m_param->lastSamplePosition.plateNumber
                    = Convert::SparkLinkProtocol_BCD2Int((uint8_t*)(p + 1), 1);
            if (m_param->lastSamplePosition.plateNumber == 3)
                m_param->lastSamplePosition.vialNumber
                        = Convert::SparkLinkProtocol_BCD2Int((uint8_t*)(p + 2), 4);
            else
            {
                m_param->lastSamplePosition.columnRowNumber.columnNumber
                        = Convert::SparkLinkProtocol_BCD2Int((uint8_t*)(p + 2), 2);
                m_param->lastSamplePosition.columnRowNumber.rowNumber
                        = Convert::SparkLinkProtocol_BCD2Int((uint8_t*)(p + 4), 2);
            }

            QVariant var;
            var.setValue(m_param->lastSamplePosition);
            emit parseCompleted(hplc::sparklink::CmdID_P_LastSamplePosition, var);
        }
        break;
    case HplcSparkLinkAutoSamplerPfcId_FlushVolume:
        {
            m_param->flushVolume
                    = Convert::SparkLinkProtocol_BCD2Int((uint8_t*)(p + 2), 4);

            QVariant var;
            var.setValue(m_param->flushVolume);
            emit parseCompleted(hplc::sparklink::CmdID_P_FlushVolume, var);
        }
        break;
    case HplcSparkLinkAutoSamplerPfcId_NumberOfInjectionsOrSample:
        {
            m_param->numberOfInjectionsOrSample
                    = Convert::SparkLinkProtocol_BCD2Int((uint8_t*)(p + 5), 1);

            QVariant var;
            var.setValue(m_param->numberOfInjectionsOrSample);
            emit parseCompleted(hplc::sparklink::CmdID_P_NumberOfInjectionsOrSample, var);
        }
        break;
    case HplcSparkLinkAutoSamplerPfcId_InjectionMode:
        {
            m_param->injectionMode
                    = (InjectionMode_t)Convert::SparkLinkProtocol_BCD2Int((uint8_t*)(p + 5), 1);

            QVariant var;
            var.setValue(m_param->injectionMode);
            emit parseCompleted(hplc::sparklink::CmdID_P_InjectionMode, var);
        }
        break;
    case HplcSparkLinkAutoSamplerPfcId_SyringeVolume:
        {
            m_param->syringeVolume
                    = Convert::SparkLinkProtocol_BCD2Int((uint8_t*)(p + 1), 5);

            QVariant var;
            var.setValue(m_param->syringeVolume);
            emit parseCompleted(hplc::sparklink::CmdID_P_SyringeVolume, var);
        }
        break;
    case HplcSparkLinkAutoSamplerPfcId_TubingVolume:
        {
            m_param->tubingVolume
                    = Convert::SparkLinkProtocol_BCD2Int((uint8_t*)(p + 3), 3);

            QVariant var;
            var.setValue(m_param->tubingVolume);
            emit parseCompleted(hplc::sparklink::CmdID_P_TubingVolume, var);
        }
        break;
    case HplcSparkLinkAutoSamplerPfcId_FirstTransportVial:
        {
            m_param->firstTransportVial.plateNumber
                    = Convert::SparkLinkProtocol_BCD2Int((uint8_t*)(p + 1), 1);
            if (m_param->firstTransportVial.plateNumber == 3)
                m_param->firstTransportVial.vialNumber
                        = Convert::SparkLinkProtocol_BCD2Int((uint8_t*)(p + 2), 4);
            else
            {
                m_param->firstTransportVial.columnRowNumber.columnNumber
                        = Convert::SparkLinkProtocol_BCD2Int((uint8_t*)(p + 2), 2);
                m_param->firstTransportVial.columnRowNumber.rowNumber
                        = Convert::SparkLinkProtocol_BCD2Int((uint8_t*)(p + 4), 2);
            }

            QVariant var;
            var.setValue(m_param->firstTransportVial);
            emit parseCompleted(hplc::sparklink::CmdID_P_FirstTransportVial, var);
        }
        break;
    case HplcSparkLinkAutoSamplerPfcId_LastTransportVial:
        {
            m_param->lastTransportVial.plateNumber
                    = Convert::SparkLinkProtocol_BCD2Int((uint8_t*)(p + 1), 1);
            if (m_param->lastTransportVial.plateNumber == 3)
                m_param->lastTransportVial.vialNumber
                        = Convert::SparkLinkProtocol_BCD2Int((uint8_t*)(p + 2), 4);
            else
            {
                m_param->lastTransportVial.columnRowNumber.columnNumber
                        = Convert::SparkLinkProtocol_BCD2Int((uint8_t*)(p + 2), 2);
                m_param->lastTransportVial.columnRowNumber.rowNumber
                        = Convert::SparkLinkProtocol_BCD2Int((uint8_t*)(p + 4), 2);
            }

            QVariant var;
            var.setValue(m_param->lastTransportVial);
            emit parseCompleted(hplc::sparklink::CmdID_P_LastTransportVial, var);
        }
        break;
    case HplcSparkLinkAutoSamplerPfcId_SampleNeedleHeight:
        {
            m_param->sampleNeedleHeight
                    = Convert::SparkLinkProtocol_BCD2Int((uint8_t*)(p + 4), 2);

            QVariant var;
            var.setValue(m_param->sampleNeedleHeight);
            emit parseCompleted(hplc::sparklink::CmdID_P_SampleNeedleHeight, var);
        }
        break;
    case HplcSparkLinkAutoSamplerPfcId_SyringeSpeed:
        {
            m_param->syringeSpeed
                    = (SyringeSpeed_t)Convert::SparkLinkProtocol_BCD2Int((uint8_t*)(p + 5), 1);

            QVariant var;
            var.setValue(m_param->syringeSpeed);
            emit parseCompleted(hplc::sparklink::CmdID_P_SyringeSpeed, var);
        }
        break;
    case HplcSparkLinkAutoSamplerPfcId_SyringeSpeedScaleFactor:
        {
            m_param->syringeSpeedScaleFactor
                    = Convert::SparkLinkProtocol_BCD2Int((uint8_t*)(p + 4), 2);

            QVariant var;
            var.setValue(m_param->syringeSpeedScaleFactor);
            emit parseCompleted(hplc::sparklink::CmdID_P_SyringeSpeedScaleFactor, var);
        }
        break;
    case HplcSparkLinkAutoSamplerPfcId_BufferVolume:
        {
            m_param->bufferVolume
                    = Convert::SparkLinkProtocol_BCD2Int((uint8_t*)(p + 2), 4);

            QVariant var;
            var.setValue(m_param->bufferVolume);
            emit parseCompleted(hplc::sparklink::CmdID_P_BufferVolume, var);
        }
        break;
#if 1 // 设备端没有，主机端有
    case HplcSparkLinkAutoSamplerPfcId_ActualSampleNumber:
        {
            m_param->actualSampleNumber.plateNumber
                    = Convert::SparkLinkProtocol_BCD2Int((uint8_t*)(p + 1), 1);
            if (m_param->actualSampleNumber.plateNumber == 3)
                m_param->actualSampleNumber.vialNumber
                        = Convert::SparkLinkProtocol_BCD2Int((uint8_t*)(p + 2), 4);
            else
            {
                m_param->actualSampleNumber.columnRowNumber.columnNumber
                        = Convert::SparkLinkProtocol_BCD2Int((uint8_t*)(p + 2), 2);
                m_param->actualSampleNumber.columnRowNumber.rowNumber
                        = Convert::SparkLinkProtocol_BCD2Int((uint8_t*)(p + 4), 2);
            }

            QVariant var;
            var.setValue(m_param->actualSampleNumber);
            emit parseCompleted(hplc::sparklink::CmdID_P_ActualSampleNumber, var);
        }
        break;
    case HplcSparkLinkAutoSamplerPfcId_Status:
        {
            m_param->status.isError
                    = Convert::SparkLinkProtocol_BCD2Int((uint8_t*)(p + 2), 1);
            m_param->status.status
                    = (Status_t)Convert::SparkLinkProtocol_BCD2Int((uint8_t*)(p + 3), 3);

            QVariant var;
            var.setValue(m_param->status);
            emit parseCompleted(hplc::sparklink::CmdID_P_Status, var);
        }
        break;
    case HplcSparkLinkAutoSamplerPfcId_ErrorCode:
        {
            m_param->_errorCode
                    = Convert::SparkLinkProtocol_BCD2Int((uint8_t*)(p + 3), 3);

            QVariant var;
            var.setValue(m_param->_errorCode);
            emit parseCompleted(hplc::sparklink::CmdID_P_ErrorCode, var);
        }
        break;
#endif
    case HplcSparkLinkAutoSamplerPfcId_ResetErrorCode:
        {
            quint8 flag
                    = Convert::SparkLinkProtocol_BCD2Int((uint8_t*)(p + 5), 1);
            if (flag == 1)
                m_param->_errorCode = 0;

            QVariant var;
            var.setValue(flag);
            emit parseCompleted(hplc::sparklink::CmdID_C_ResetErrorCode, var);
        }
        break;
    case HplcSparkLinkAutoSamplerPfcId_AirSegment:
        {
            m_param->airSegment
                    = Convert::SparkLinkProtocol_BCD2Int((uint8_t*)(p + 5), 1);

            QVariant var;
            var.setValue(m_param->airSegment);
            emit parseCompleted(hplc::sparklink::CmdID_P_AirSegment, var);
        }
        break;
    case HplcSparkLinkAutoSamplerPfcId_SkipMissingSamplePosition:
        {
            m_param->skipMissingSamplePosition
                    = Convert::SparkLinkProtocol_BCD2Int((uint8_t*)(p + 5), 1);

            QVariant var;
            var.setValue(m_param->skipMissingSamplePosition);
            emit parseCompleted(hplc::sparklink::CmdID_P_SkipMissingSamplePosition, var);
        }
        break;
    case HplcSparkLinkAutoSamplerPfcId_HeadSpacePressure:
        {
            m_param->headSpacePressure
                    = Convert::SparkLinkProtocol_BCD2Int((uint8_t*)(p + 5), 1);

            QVariant var;
            var.setValue(m_param->headSpacePressure);
            emit parseCompleted(hplc::sparklink::CmdID_P_HeadSpacePressure, var);
        }
        break;
    case HplcSparkLinkAutoSamplerPfcId_ResetOutputAfterSerials:
        {
            m_param->resetOutputAfterSerials
                    = Convert::SparkLinkProtocol_BCD2Int((uint8_t*)(p + 5), 1);

            QVariant var;
            var.setValue(m_param->resetOutputAfterSerials);
            emit parseCompleted(hplc::sparklink::CmdID_P_ResetOutputAfterSerials, var);
        }
        break;
    case HplcSparkLinkAutoSamplerPfcId_UsePrepMode:
        {
            m_param->usePrepMode
                    = Convert::SparkLinkProtocol_BCD2Int((uint8_t*)(p + 5), 1);

            QVariant var;
            var.setValue(m_param->usePrepMode);
            emit parseCompleted(hplc::sparklink::CmdID_P_UsePrepMode, var);
        }
        break;
    case HplcSparkLinkAutoSamplerPfcId_TraySegmentSettings:
        {
            m_param->traySegmentSettings.leftTrayType
                    = Convert::SparkLinkProtocol_BCD2Int((uint8_t*)(p + 2), 1);
            m_param->traySegmentSettings.rightTrayType
                    = Convert::SparkLinkProtocol_BCD2Int((uint8_t*)(p + 3), 1);
            if (m_param->traySegmentSettings.leftTrayType == 3
                    || m_param->traySegmentSettings.rightTrayType == 3)
            {
                m_param->traySegmentSettings.singlePlateType
                    = Convert::SparkLinkProtocol_BCD2Int((uint8_t*)(p + 4), 2);
            }
            else
            {
                m_param->traySegmentSettings.separatePlateType.leftType
                    = Convert::SparkLinkProtocol_BCD2Int((uint8_t*)(p + 4), 1);
                m_param->traySegmentSettings.separatePlateType.rightType
                    = Convert::SparkLinkProtocol_BCD2Int((uint8_t*)(p + 5), 1);
            }

            QVariant var;
            var.setValue(m_param->traySegmentSettings);
            emit parseCompleted(hplc::sparklink::CmdID_P_TraySegmentSettings, var);
        }
        break;
    case HplcSparkLinkAutoSamplerPfcId_SyncCondition:
        {
            m_param->syncCondition.isSyncWithValveWash
                    = Convert::SparkLinkProtocol_BCD2Int((uint8_t*)(p + 2), 1);
            m_param->syncCondition.isSyncWithInject
                    = Convert::SparkLinkProtocol_BCD2Int((uint8_t*)(p + 3), 1);
            m_param->syncCondition.isSyncWithLoad
                    = Convert::SparkLinkProtocol_BCD2Int((uint8_t*)(p + 4), 1);
            m_param->syncCondition.isSyncWithNextInjection
                    = Convert::SparkLinkProtocol_BCD2Int((uint8_t*)(p + 5), 1);

            QVariant var;
            var.setValue(m_param->syncCondition);
            emit parseCompleted(hplc::sparklink::CmdID_P_SyncCondition, var);
        }
        break;
    case HplcSparkLinkAutoSamplerPfcId_InjectionVolume:
        {
            m_param->injectionVolume
                    = Convert::SparkLinkProtocol_BCD2Int((uint8_t*)(p + 1), 5);

            QVariant var;
            var.setValue(m_param->injectionVolume);
            emit parseCompleted(hplc::sparklink::CmdID_P_InjectionVolume, var);
        }
        break;
    case HplcSparkLinkAutoSamplerPfcId_WashBetween:
        {
            m_param->washBetween
                    = (WashBetween_t)Convert::SparkLinkProtocol_BCD2Int((uint8_t*)(p + 5), 1);

            QVariant var;
            var.setValue(m_param->washBetween);
            emit parseCompleted(hplc::sparklink::CmdID_P_WashBetween, var);
        }
        break;
    case HplcSparkLinkAutoSamplerPfcId_WashTimes:
        {
            m_param->washTimes.steps = ai;
            m_param->washTimes.times
                    = Convert::SparkLinkProtocol_BCD2Int((uint8_t*)(p + 5), 1);

            QVariant var;
            var.setValue(m_param->washTimes);
            emit parseCompleted(hplc::sparklink::CmdID_P_WashTimes, var);
        }
        break;
    case HplcSparkLinkAutoSamplerPfcId_WashSyringeValveSolventPort:
        {
            m_param->washSyringeValveSolventPort.steps = ai;
            m_param->washSyringeValveSolventPort.port
                    = (WashSyringeValveSolventPort_t)Convert::SparkLinkProtocol_BCD2Int((uint8_t*)(p + 5), 1);

            QVariant var;
            var.setValue(m_param->washSyringeValveSolventPort);
            emit parseCompleted(hplc::sparklink::CmdID_P_WashSyringeValveSolventPort, var);
        }
        break;
    case HplcSparkLinkAutoSamplerPfcId_FillTransportPositionTimes:
        {
            m_param->fillTransportPositionTimes.port
                    = Convert::SparkLinkProtocol_BCD2Int((uint8_t*)(p + 3), 1);
            m_param->fillTransportPositionTimes.times
                    = Convert::SparkLinkProtocol_BCD2Int((uint8_t*)(p + 5), 1);

            QVariant var;
            var.setValue(m_param->fillTransportPositionTimes);
            emit parseCompleted(hplc::sparklink::CmdID_P_FillTransportPositionTimes, var);
        }
        break;
    case HplcSparkLinkAutoSamplerPfcId_WashVolume:
        {
            m_param->washVolume.steps = ai;
            m_param->washVolume.volume
                    = Convert::SparkLinkProtocol_BCD2Int((uint8_t*)(p + 2), 4);

            QVariant var;
            var.setValue(m_param->washVolume);
            emit parseCompleted(hplc::sparklink::CmdID_P_WashVolume, var);
        }
        break;
    case HplcSparkLinkAutoSamplerPfcId_UseInMethod:
        {
            m_param->useInMethod.isUseTimedEvents
                    = Convert::SparkLinkProtocol_BCD2Int((uint8_t*)(p + 4), 1);
            m_param->useInMethod.isUseMix
                    = Convert::SparkLinkProtocol_BCD2Int((uint8_t*)(p + 5), 1);

            QVariant var;
            var.setValue(m_param->useInMethod);
            emit parseCompleted(hplc::sparklink::CmdID_P_UseInMethod, var);
        }
        break;

    case HplcSparkLinkAutoSamplerPfcId_SerialNumber:
        {
            m_param->_serialNumber
                    = Convert::SparkLinkProtocol_BCD2Int((uint8_t*)(p + 0), 6);

            QVariant var;
            var.setValue(m_param->_serialNumber);
            emit parseCompleted(hplc::sparklink::CmdID_P_SerialNumber, var);
        }
        break;
#if 1 // 设备端没有
    case HplcSparkLinkAutoSamplerPfcId_DoorSensor:
        {
            m_param->doorSensor
                    = Convert::SparkLinkProtocol_BCD2Int((uint8_t*)(p + 5), 1);

            QVariant var;
            var.setValue(m_param->doorSensor);
            emit parseCompleted(hplc::sparklink::CmdID_P_DoorSensor, var);
        }
        break;
#endif
    default:
        break;
    }
}


} // sparklink
} // hplc
